package www.dianbaozu.com.ui.my

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentModifyPhoneStep1Binding
import www.dianbaozu.com.extension.requestUniversally
import www.dianbaozu.com.ui.ProxyViewModel
import www.wen.com.common.lib.base.fragment.BaseReceiveSMSFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.NEXT_STEP_OR_PREVIOUS_KEY
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description: 修改手机
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class ModifyPhoneStepOneFragment : BaseReceiveSMSFragment<FragmentModifyPhoneStep1Binding>(), View.OnClickListener{

    private val mModifyPhoneViewModel: ModifyPhoneViewModel by viewModels()

    private val proxyViewModel: ProxyViewModel by activityViewModels()

    override val viewModel: BaseViewModel
        get() = mModifyPhoneViewModel

    override fun isEnableLoadingHelper(): Boolean = false

    override fun getLayoutId(): Int = R.layout.fragment_modify_phone_step_1

    override fun lazyInitView() {
        super.lazyInitView()
        dataBinding.run {
            proxyViewModel = this@ModifyPhoneStepOneFragment.proxyViewModel
            viewModel = mModifyPhoneViewModel
            tvPhoneNumber.setText(SpUtil.decodeString(KeyHelper.MMKV_MOBILE))
        }
    }




    override fun onClick(v: View) {
        when(v.id){
            R.id.btnLogin->{
                nextStep()
            }
            R.id.slVerificationCode -> {
                sendVerificationCode(
                        dataBinding.tvPhoneNumber,
                        dataBinding.etVerificationCode,
                        proxyViewModel.itac.value!!,
                        mModifyPhoneViewModel
                )
            }
            R.id.tvCountryAreaCode -> {
                showAreaPopupView()
            }
        }
    }

    private fun nextStep() {
        mModifyPhoneViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = "",
                isShowToastTip = false,
                request = { appDataManage ->
                    appDataManage.requestVerifyMobile(
                            RequestBodyParameter(
                                    mobile = dataBinding.tvPhoneNumber.text.toString(),
                                    smsCode = dataBinding.etVerificationCode.text.toString(),
                            ))
                }
        ) {
            postObject(NEXT_STEP_OR_PREVIOUS_KEY,true)
            nav(R.id.action_modifyPhoneStepOneFragment_to_modifyPhoneStepTwoFragment)
        }
    }

    override fun getRecordTimeKey(): String = KeyHelper.MODIFY_PHONE_STEP_ONE_REMAINDER_TIME

    override fun getSystemRecordTimeKey(): String = KeyHelper.MODIFY_PHONE_STEP_ONE_SYSTEM_REMAINDER_TIME

    private fun showAreaPopupView() {
        CreateXPopupHelper.createBottomAreaCodeListPopupView(PopupViewEntity().apply {
            this.context = requireActivity()
            isShow = true
            itac = proxyViewModel.itac.value!!
            areaCodeList = proxyViewModel.areaCodeEntities.value
            areaCodeListResultCallBack = {
                "+${it.code}".also { area ->
                    dataBinding.tvCountryAreaCode.text = area
                }
                proxyViewModel.itac.value = it.code
                SpUtil.encode(KeyHelper.MMKV_ITAC, it.code)
            }
        })
    }


}



